package com.xiaoshuidi.cloud.module.contract.service.approval;

import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractActiveAssociationCreateReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractActiveAssociationUpdateReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.approval.ContractActiveAssociationConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.approval.ContractActiveAssociationDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.approval.ContractActiveAssociationMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants.ACTIVE_ASSOCIATION_NOT_EXISTS;

/**
 * 业务和审批流关联 Service 实现类
 *
 * @author 合房租赁
 */
@Service
public class ContractActiveAssociationServiceImpl implements ContractActiveAssociationService {

    @Resource
    private ContractActiveAssociationMapper contractActiveAssociationMapper;

    @Override
    public Long createActiveAssociation(ContractActiveAssociationCreateReqVO createReqVO) {
        // 插入
        ContractActiveAssociationDO activeAssociation = ContractActiveAssociationConvert.INSTANCE.convert(createReqVO);
        contractActiveAssociationMapper.insert(activeAssociation);
        // 返回
        return activeAssociation.getId();
    }

    @Override
    public void updateActiveAssociation(ContractActiveAssociationUpdateReqVO updateReqVO) {
        // 校验存在
        validateActiveAssociationExists(updateReqVO.getId());
        // 更新
        ContractActiveAssociationDO updateObj = ContractActiveAssociationConvert.INSTANCE.convert(updateReqVO);
        contractActiveAssociationMapper.updateById(updateObj);
    }

    @Override
    public void deleteActiveAssociation(Long id) {
        // 校验存在
        validateActiveAssociationExists(id);
        // 删除
        contractActiveAssociationMapper.deleteById(id);
    }

    private void validateActiveAssociationExists(Long id) {
        if (contractActiveAssociationMapper.selectById(id) == null) {
            throw exception(ACTIVE_ASSOCIATION_NOT_EXISTS);
        }
    }

    @Override
    public ContractActiveAssociationDO getActiveAssociation(Long id) {
        return contractActiveAssociationMapper.selectById(id);
    }

    @Override
    public List<ContractActiveAssociationDO> getActiveAssociationList(Collection<Long> ids) {
        return contractActiveAssociationMapper.selectBatchIds(ids);
    }

}
